<template>
    <div>
        <el-form ref="form" :model="obj.data" label-width="110px">
            <el-form-item label="产品名称" prop="name" :rules="[formRulesMixin.notNull]">
                <el-input v-model="obj.data.name" clearable autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="保险公司名称" prop="companyname" :rules="[formRulesMixin.notNull]">
                <el-input v-model="obj.data.companyname" clearable autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="联系人" prop="man" :rules="[formRulesMixin.notNull]">
                <el-input v-model="obj.data.man" clearable autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="联系电话" prop="mobile" :rules="[formRulesMixin.notNull]">
                <el-input v-model="obj.data.mobile" clearable autocomplete="off" maxlength="11" show-word-limit></el-input>
            </el-form-item>
            <el-form-item label="价格" prop="price" :rules="[formRulesMixin.notNull]">
                <el-input type="number" v-model="obj.data.price" clearable autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="封面">
                <UpFileS v-model:file="obj.data.cover" dir="insurance" />
            </el-form-item>
            <el-form-item label="产品介绍" prop="msg" :rules="[formRulesMixin.notNull]">
                <Editer v-model:value="obj.data.msg" width="100%" height="60vh" :maxheight="100000" />
            </el-form-item>
            <el-form-item class="dialog-btns-item">
                <div class="dialog-btns">
                    <span class="btn clear" @click="$emit('close')">取消</span>
                    <span class="btn sub ml10" @click.prevent="upData">提交</span>
                </div>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup>
    import { reactive,ref,getCurrentInstance,toRaw,inject } from 'vue'
    import UpFileS from "@/components/upfile_single.vue"
    import Editer from "@/components/editer.vue"
    let { proxy } = getCurrentInstance()
    
    let obj = reactive({
        data:{
            insurance_id: inject("id").value || 0,
            name: "",
            companyname:"",
            man:"",
            mobile:"",
            price:"",
            cover_old: "",
            cover:"",
            msg: "",
        },
    })

    let init = async () => {
        if(obj.data.insurance_id > 0){
            proxy.$ajax({
                url: 'insurance/get',
                data: obj.data,
                bfLoading: true
            }).then(d => {
                if(d.success){
                    obj.data = {...obj.data, ...d.msg}
                    obj.data.cover_old = d.msg.cover
                }else{
                    proxy.elMessageMixin(d.err, 'error')
                }
            }).catch(() => { })
        }
    }
    init()
    let upData = () => {
        proxy.$refs['form'].validate(valid => {
            if (valid) {
                proxy.$ajax({
                    url: 'insurance/design',
                    data: obj.data,
                    bfLoading: true,
                    timeout:60000
                }).then(d => {
                    if(d.success){
                        proxy.elMessageMixin(`${obj.data.insurance_id>0?'修改':"添加"}成功`, 'success')
                        proxy.$emit('init')
                        proxy.$emit('close')
                    }else{
                        proxy.elMessageMixin(d.err, 'error')
                    }
                }).catch(() => { })
            }else{
                return false
            }
        })
    }
</script>

<style scoped>
</style>